//
//  ViewController.m
//  HACShield
//
//  Created by Chengjie on 2019/5/6.
//  Copyright © 2019 Happy. All rights reserved.
//

#import "ViewController.h"
#import <objc/runtime.h>

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)tryTest:(UIButton *)sender {
    [self testArrayClass];
    [self testMutableArrayClass];
    [self testDictionaryClass];
    [self testUrlCreate];
    [self testMutableDictionaryClass];
    [self testImageView];
    [self testSetClass];
    [self testForwardingMessage];
    [self testStringClass];
    [self testMutableStringClass];
    [self testStringClassMethodist];
    [self testMutableStringClassMethodist];
    [self testAttributeStringClassMethodist];
    [self testMutableAttributeStringClassMethodist];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view isEqual:nil];
}

- (void)testArrayClass {
    NSArray *commonArr = [NSArray alloc];
    NSArray *arr01 = [commonArr init];
    NSArray *arr02 = [commonArr initWithObjects:@"1", nil];
    NSArray *arr03 = [commonArr initWithObjects:@"1", @"2", nil];
    NSArray *arr04 = [commonArr initWithObjects:@"1", @"2", @"3", nil];
    NSLog(@"commonArr: %s", object_getClassName(commonArr));
    NSLog(@"arr01: %s", object_getClassName(arr01));
    NSLog(@"arr02: %s", object_getClassName(arr02));
    NSLog(@"arr03: %s", object_getClassName(arr03));
    NSLog(@"arr04: %s", object_getClassName(arr04));
    // 测试交换成功
    [arr01 objectAtIndex:10];
    [arr02 objectAtIndex:10];
    [arr03 objectAtIndex:10];
    [arr04 objectAtIndex:10];

    for (NSInteger index = 0; index < arr04.count; index++) {
        NSString *number = [arr04 objectAtIndex:index];
        NSLog(@"%@", number);
    }
    NSLog(@"%s", __func__);
}

- (void)testMutableArrayClass {
    NSLog(@"%s", __func__);
    NSMutableArray *commonArr = [NSMutableArray alloc];
    NSMutableArray *arr01 = [commonArr init];
    NSMutableArray *arr_copy0 = @[].mutableCopy;
    NSMutableArray *arr_copy1 = [commonArr initWithObjects:@"1", nil].mutableCopy;
    NSLog(@"%s--%s--%s--%s", object_getClassName(commonArr), object_getClassName(arr01), object_getClassName(arr_copy0), object_getClassName(arr_copy1));
    [arr_copy1 objectAtIndex:10];
    for (NSInteger index = 0; index < arr_copy1.count; index++) {
        NSString *number = [arr_copy1 objectAtIndex:index];
        NSLog(@"%@", number);
    }
    NSMutableArray *er_Arr = @[];
    [er_Arr addObject:@"1"];
    [er_Arr exchangeObjectAtIndex:1 withObjectAtIndex:2];
    [er_Arr insertObject:@"x" atIndex:2];
    [er_Arr replaceObjectAtIndex:10 withObject:@"xx"];
    [er_Arr removeObjectAtIndex:10];
    
    [arr01 addObject:nil];
    [arr01 insertObject:@"xxx" atIndex:10];
    [arr01 insertObject:nil atIndex:0];
    [arr01 replaceObjectAtIndex:10 withObject:@"x"];
    [arr01 exchangeObjectAtIndex:10 withObjectAtIndex:1];
    [arr01 removeObjectAtIndex:10];
    
    NSLog(@"%s", __func__);
}


- (void)testDictionaryClass {
    //    __NSDictionary0
    NSMutableDictionary *dict = @{};
    //    __NSSingleEntryDictionaryI
    NSMutableDictionary *dict1 = @{@"key":@"xxx"};
    //    __NSDictionaryI
    NSMutableDictionary *dict2 = @{@"key":@"xxx",
                                  @"key1":@"xxx信息"
                                  };
    NSMutableDictionary *dict3 = [NSDictionary dictionary];
    NSMutableDictionary *dict4 = [NSDictionary dictionaryWithObjectsAndKeys:@"xxx", @"key", nil];
    NSMutableDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"xxx", @"key", @"xxkx", @"zhiahha", nil];
    [dict setObject:@"xxxxx" forKey:@"key"];
    [dict4 setObject:[UIView new] forKey:@"key"];
    [dict2 setObject:@"xxxxx" forKey:@"key"];
    [dict3 setObject:@"xxxxx" forKey:@"key"];
    [dict4 setObject:@"xxxxx" forKey:@"key"];
    [dict5 setObject:@"xxxxx" forKey:@"key"];
    [dict setObject:@"xxxxx" forKey:@"key"];
    [dict setObject:@"xxxxx" forKey:@"key"];
    [dict4 setObject:[UIView new] forKey:@"key"];
    [dict2 setObject:@"xxxxx" forKey:@"key"];
    [dict3 setObject:@"xxxxx" forKey:@"key"];
    NSLog(@"%@",dict);
}

- (void)testMutableDictionaryClass {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSLog(@"NSMutableDictionary ===> %s", object_getClassName(dict));
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithCapacity:10];
    NSLog(@"NSMutableDictionary1 ===> %s", object_getClassName(dict1));
}

- (void)testUrlCreate {
    NSLog(@"%s", __func__);
    NSURL *url = [NSURL URLWithString:nil];
    NSURL *url1 = [[NSURL alloc] initWithString:nil];
    NSURL *url2 = [[NSURL alloc] initWithString:url];
    NSURL *url3 = [NSURL URLWithString:url1];
    NSLog(@"%s", __func__);
    NSLog(@"%s", __func__);
}

- (void)testImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:nil];
    UIImageView *imageView1 = [[UIImageView alloc] init];
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:self.view.bounds];
    NSLog(@"%@ -- %@ -- %@", imageView, imageView1, imageView2);
}

- (void)testSetClass {
    {
        NSSet *set = [NSSet set];
        NSLog(@"NSSet0 ===> %s", object_getClassName(set));
        NSSet *set1 = [NSSet setWithObject:@(0)];
        NSLog(@"NSSet1 ===> %s", object_getClassName(set1));
        NSSet *set2 = [NSSet setWithObjects:@(0), @"x", [UIView new], nil];
        NSLog(@"NSSet2 ===> %s", object_getClassName(set2));
        NSSet *set3 = [NSSet setWithArray:@[@(0), @"x", [UIView new],]];
        NSLog(@"NSSet3 ===> %s", object_getClassName(set3));
        NSSet *set4 = [NSSet setWithArray:@[]];
        NSLog(@"NSSet4 ===> %s", object_getClassName(set4));
        NSSet *set5 = [[NSSet alloc] init];
        NSLog(@"NSSet5 ===> %s", object_getClassName(set5));
    }
    {
        NSMutableSet *set = [NSMutableSet setWithArray:@[@(0), @"x", [UIView new],]];
        NSLog(@"NSMutableSet0 ===> %s", object_getClassName(set));
        NSMutableSet *set1 = [NSMutableSet setWithObject:@(0)];
        NSLog(@"NSMutableSet1 ===> %s", object_getClassName(set1));
        NSMutableSet *set2 = [NSMutableSet setWithObjects:@(0), @"x", [UIView new], nil];
        NSLog(@"NSMutableSet2 ===> %s", object_getClassName(set2));
        NSSet *set3 = [[NSMutableSet alloc] init];
        NSLog(@"NNSMutableSet3 ===> %s", object_getClassName(set3));
    }
    {
        NSMutableSet *set = [NSSet set];
        NSLog(@"NSSet0 ===> %s", object_getClassName(set));
        [set addObject:@"x"];
        [set addObjectsFromArray:@[@(0),@(1),]];
        [set removeObject:@(3)];
    }
    {
        NSMutableSet *set = [NSMutableSet set];
        NSLog(@"NSSet0 ===> %s", object_getClassName(set));
        [set addObject:nil];
        [set addObjectsFromArray:nil];
        [set removeObject:nil];
    }
}

- (void)testForwardingMessage {
    NSNull *null = [NSNull null];
    NSString *str = null;
    NSNumber *number = null;
    NSArray *array = null;
    NSUInteger len =  str.length;
    NSLog(@"testNullClass: %ld", len);
    NSLog(@"testNullClass: %@", number.stringValue);
    NSLog(@"testNullClass: %ld", array.count);
    [self performSelector:@selector(testClassMessage) withObject:self];
    [[self class] performSelector:@selector(testClassMessage) withObject:self];
}

- (void)testStringClass {
    {
        NSString *str0 = @(0).stringValue;
        NSString *str1 = @"";
        NSString *str2 = [[NSString alloc] init];
        NSString *str3 = [[NSString alloc] initWithFormat:@"xxx"];
        NSString *str4 = [NSString stringWithFormat:@"%@", NSStringFromClass([self class])];
        NSString *str5 = [NSString stringWithString:str3];
        NSString *str6 = [NSString stringWithString:str4];
        NSString *str_a = [NSString alloc];
        NSLog(@"str0 ===> %s", object_getClassName(str0));
        NSLog(@"str1 ===> %s", object_getClassName(str1));
        NSLog(@"str2 ===> %s", object_getClassName(str2));
        NSLog(@"str3 ===> %s", object_getClassName(str3));
        NSLog(@"str4 ===> %s", object_getClassName(str4));
        NSLog(@"str5 ===> %s", object_getClassName(str5));
        NSLog(@"str6 ===> %s", object_getClassName(str6));
        NSLog(@"str_a ===> %s", object_getClassName(str_a));
    }
    {
        NSString *str0 = @"我猜";
        NSString *str_str = [NSString stringWithString:str0];
        NSLog(@"str_str ===> %s", object_getClassName(str_str));
        NSString *str_str_0 = [NSString stringWithString:nil];
        NSLog(@"str_str_str_0str ===> %s", object_getClassName(str_str_0));
        NSString *str_str_1 = [[NSString alloc] initWithString:nil];
        NSLog(@"str_str_1 ===> %s", object_getClassName(str_str_1));
    }
}

- (void)testMutableStringClass {
    {
        NSLog(@"mutableStr-1 ===> %s", object_getClassName([NSMutableString alloc]));
        NSLog(@"mutableStr-1 ===> %@", NSStringFromClass(NSClassFromString(@"NSPlaceholderMutableString").superclass.superclass));
        NSMutableString *mutableStr0 = [[NSMutableString alloc] initWithString:nil];
        NSMutableString *mutableStr1 = [NSMutableString stringWithString:nil];
        NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"%@", @"wahahaha"];
        NSLog(@"mutableStr0 ===> %s", object_getClassName(mutableStr0));
        NSLog(@"mutableStr1 ===> %s", object_getClassName(mutableStr1));
        NSLog(@"mutableStr2 ===> %s", object_getClassName(mutableStr2));
        [mutableStr2 appendString:@"wuwuwuw"];
        NSLog(@"mutableStr2 ===> %s", object_getClassName(mutableStr2));
    }
    {
        NSString *place_holder_str = [NSString alloc];
        NSUInteger forCount = 20;
        Class cls = place_holder_str.class;
        do {
            NSLog(@"NSString =CLS=> %@", NSStringFromClass(cls));
            cls = place_holder_str.superclass;
            forCount--;
        } while (forCount);
    }
    
    {
        NSMutableString *place_holder_str = [NSMutableString alloc];
        NSUInteger forCount = 20;
        Class cls = place_holder_str.class;
        do {
            NSLog(@"NSMutableString =CLS=> %@", NSStringFromClass(cls));
            cls = place_holder_str.superclass;
            forCount--;
        } while (forCount);
    }
    {
        NSString *str0 = @"我猜";
        NSString *str_str = [NSString stringWithString:str0];
        NSMutableString *mutableStr0 = [[NSMutableString alloc] initWithString:nil];
        NSLog(@"str_str ===> %s", object_getClassName(str_str));
        NSString *str_str_0 = [NSString stringWithString:nil];
        NSMutableString *mutableStr1 = [NSMutableString stringWithString:nil];
        NSLog(@"str_str_str_0str ===> %s", object_getClassName(str_str_0));
        NSString *str_str_1 = [[NSString alloc] initWithString:nil];
        NSLog(@"str_str_1 ===> %s", object_getClassName(str_str_1));
    }
}

- (void)testStringClassMethodist {
    [@"10" substringFromIndex:1];
    [@"我猜你" substringFromIndex:10];
    [@"10" substringFromIndex:10];
    [@"我猜你" substringFromIndex:1];
    NSString *temp = [@"我猜你我猜你我猜你我猜你我猜你" stringByAppendingFormat:@"%d", arc4random()];
    [[NSString stringWithFormat:@"%@", temp] substringFromIndex:5];
    [[NSString stringWithFormat:@"%@", temp] substringFromIndex:100];
    
}

- (void)testMutableStringClassMethodist {
    [[NSMutableString stringWithString:@"10"] substringFromIndex:1];
    [[NSMutableString stringWithString:@"我猜你"] substringFromIndex:10];
    [[NSMutableString stringWithString:@"10"] substringFromIndex:10];
    [[NSMutableString stringWithString:@"我猜你"] substringFromIndex:1];
    NSString *temp = [@"我猜你我猜你我猜你我猜你我猜你" stringByAppendingFormat:@"%d", arc4random()];
    [[NSMutableString stringWithFormat:@"%@", temp] substringFromIndex:5];
    [[NSMutableString stringWithFormat:@"%@", temp] substringFromIndex:100];
    
    [[NSMutableString stringWithFormat:@"%@", temp] deleteCharactersInRange:NSMakeRange(0, 100)];
}

- (void)testAttributeStringClassMethodist {
    NSAttributedString *attr0 = [[NSAttributedString alloc] init];
    NSAttributedString *attr1 = [[NSAttributedString alloc] initWithString:@""];
    NSAttributedString *attr2 = [[NSAttributedString alloc] initWithString:@"xxxx"];
    NSAttributedString *attr3 = [[NSAttributedString alloc] initWithAttributedString:attr2];
    NSLog(@"attr0 == > %s", object_getClassName(attr0));
    NSLog(@"attr1 == > %s", object_getClassName(attr1));
    NSLog(@"attr2 == > %s", object_getClassName(attr2));
    NSLog(@"attr3 == > %s", object_getClassName(attr3));
    NSLog(@"attr4 == > %s", object_getClassName([NSAttributedString alloc]));
    NSLog(@"attr4 == > %s", object_getClassName([NSAttributedString alloc].superclass));
}

- (void)testMutableAttributeStringClassMethodist {
    NSMutableAttributedString *attr0 = [[NSMutableAttributedString alloc] init];
    NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc] initWithString:@""];
    NSMutableAttributedString *attr2 = [[NSMutableAttributedString alloc] initWithString:@"xxxx"];
    NSMutableAttributedString *attr3 = [[NSMutableAttributedString alloc] initWithAttributedString:attr2];
    NSLog(@"mutable_attr0 == > %s", object_getClassName(attr0));
    NSLog(@"mutable_attr1 == > %s", object_getClassName(attr1));
    NSLog(@"mutable_attr2 == > %s", object_getClassName(attr2));
    NSLog(@"mutable_attr3 == > %s", object_getClassName(attr3));
    NSLog(@"mutable_attr4 == > %s", object_getClassName([NSMutableAttributedString alloc]));
    NSLog(@"mutable_attr4 == > %s", object_getClassName([NSMutableAttributedString alloc].superclass));
}

@end
